package com.ruoyi.modeling.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.MapHumpUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.modeling.domain.ModelTable;
import com.ruoyi.modeling.domain.ModelTableColumn;
import com.ruoyi.modeling.domain.WfFormBo;
import com.ruoyi.modeling.mapper.BuildTableMapper;
import com.ruoyi.modeling.service.IBuildTableService;

/**
 * 流程表单Service业务层处理
 *
 * @author KonBAI
 * @createTime 2022/3/7 22:07
 */
@Service
public class BuildTableServiceImpl implements IBuildTableService {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Autowired
    private BuildTableMapper buildTableMapper;

    @Override
    public void buildTableByWfForm(WfFormBo form) {

        String content = form.getContent();

        JSONObject formJson = JSONObject.parseObject(content);
        String formRef = formJson.getString("formRef");
        String formModel = formJson.getString("formModel");

        JSONArray fields = formJson.getJSONArray("fields");

        if(fields == null || fields.size() == 0){
            logger.warn("表单缺少字段信息,无法建表");
            return;
        }

        ModelTable table = new ModelTable();
        table.setTableName(StringUtils.toUnderScoreCase(formModel));
        table.setTableComment(form.getFormName());

        List<ModelTableColumn> columnList = new ArrayList<ModelTableColumn>();

        for(int i = 0;i<fields.size();i++){
            JSONObject fieldJson = fields.getJSONObject(i);
            JSONObject fieldConfig = fieldJson.getJSONObject("__config__");
            String label = fieldConfig.getString("label");
            String formId = fieldConfig.getString("formId");
            String vModel = fieldJson.getString("__vModel__");

            logger.info("字段信息label={},formId={},vModel={}",label,formId,vModel);

            ModelTableColumn column = new ModelTableColumn();
            column.setColumnName(StringUtils.toUnderScoreCase(vModel));
            column.setColumnComment(label);
            //TODO 待实现字段动态类型
            column.setColumnType("varchar(40)");

            columnList.add(column);

        }
        table.setColumns(columnList);

        buildTableMapper.updateBuildTableByWfForm(table);


    }

    @Override
    public TableDataInfo queryPageList(String table, Map<String,String> paramMap) {

        //TODO 待实现动态查询条件中分页条件和排序条件,其中排序依赖于表单设计
        List<Map<String,Object>> list = buildTableMapper.selectTableData(table,paramMap,null);
        TableDataInfo tableDataInfo = new TableDataInfo();

        tableDataInfo.setRows(MapHumpUtils.toCamelCase(list));
        tableDataInfo.setTotal(100);

        return tableDataInfo;
    }

    @Override
    public Map<String, String> selectById(String tableName, Long id) {
        Map map = buildTableMapper.selectTableDataById(tableName,id);
        return MapHumpUtils.toCamelCase(map);
    }

}
